/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import java.util.ArrayList;

import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;

/**
 * Defines the command keys.
 */
public class Command
{
  /** Constants */
  public final static int CMD_NONE = 0;

  public final static int CMD_ZOOM_IN = 1;
  public final static int CMD_ZOOM_OUT = 2;
  public final static int CMD_ZOOM_LOCATION = 3;

  public final static int CMD_CHANGE_MODE = 4;

  public final static int CMD_TRACK_HISTORY = 5;
  public final static int CMD_TRACK_DETAILS = 6;
  public final static int CMD_PREFERENCES = 7;

  public final static int CMD_TRACK_START = 8;
  public final static int CMD_TRACK_SAVE = 9;
  public final static int CMD_TRACK_EDIT = 10;
  public final static int CMD_TRACK_DELETE = 11;
  public final static int CMD_TRACK_PAUSE = 12;
  public final static int CMD_TRACK_RESUME = 13;
  public final static int CMD_TRACK_STOP = 14;
  public final static int CMD_TRACK_SHOW = 15;
  public final static int CMD_TRACK_HIDE = 16;
  public final static int CMD_ABOUT = 17;
  public final static int CMD_TOOLS = 18;

  public final static int CMD_DATA_EXPORT_TRACK = 19;

  // Modes
  public final static int CMD_MODE_MAP = 100;
  public final static int CMD_MODE_TRACKS = 101;
  public final static int CMD_MODE_INFO = 102;

  /** Fields */
  public int id = CMD_NONE;
  public String name = "";

  // Commands list
  private static ArrayList<Command> vecCommand = new ArrayList<Command>();

  /**
   * Default constructor.
   *
   * @param context
   * @param id
   * @param idResName
   */
  private Command(Context context, int id, int resId)
  {
    this.id = id;
    name = context.getString(resId);
  }

  /**
   * Initializes the global commands.
   *
   * @param context
   */
  public static void init(Context context)
  {
    vecCommand.add(new Command(context, Command.CMD_ZOOM_IN, R.string.cmd_zoom_in));
    vecCommand.add(new Command(context, Command.CMD_ZOOM_OUT, R.string.cmd_zoom_out));
    vecCommand.add(new Command(context, Command.CMD_ZOOM_LOCATION, R.string.cmd_zoom_location));
    vecCommand.add(new Command(context, Command.CMD_CHANGE_MODE, R.string.cmd_change_mode));
    vecCommand.add(new Command(context, Command.CMD_TRACK_HISTORY, R.string.cmd_track_history));
    vecCommand.add(new Command(context, Command.CMD_TRACK_DETAILS, R.string.cmd_track_details));
    vecCommand.add(new Command(context, Command.CMD_PREFERENCES, R.string.cmd_preferences));
    vecCommand.add(new Command(context, Command.CMD_TRACK_START, R.string.cmd_track_start));
    vecCommand.add(new Command(context, Command.CMD_TRACK_SAVE, R.string.cmd_track_save));
    vecCommand.add(new Command(context, Command.CMD_TRACK_EDIT, R.string.cmd_track_edit));
    vecCommand.add(new Command(context, Command.CMD_TRACK_DELETE, R.string.cmd_track_delete));
    vecCommand.add(new Command(context, Command.CMD_TRACK_PAUSE, R.string.cmd_track_pause));
    vecCommand.add(new Command(context, Command.CMD_TRACK_RESUME, R.string.cmd_track_resume));
    vecCommand.add(new Command(context, Command.CMD_TRACK_STOP, R.string.cmd_track_stop));
    vecCommand.add(new Command(context, Command.CMD_TRACK_SHOW, R.string.cmd_track_show));
    vecCommand.add(new Command(context, Command.CMD_TRACK_HIDE, R.string.cmd_track_hide));
    vecCommand.add(new Command(context, Command.CMD_ABOUT, R.string.cmd_about));
    vecCommand.add(new Command(context, Command.CMD_TOOLS, R.string.cmd_tools));
    vecCommand.add(new Command(context, Command.CMD_DATA_EXPORT_TRACK, R.string.cmd_data_export));
    vecCommand.add(new Command(context, Command.CMD_MODE_TRACKS, R.string.cmd_mode_tracks));
    vecCommand.add(new Command(context, Command.CMD_MODE_MAP, R.string.cmd_mode_map));
    vecCommand.add(new Command(context, Command.CMD_MODE_INFO, R.string.cmd_mode_info));
  }

  /**
   * Retrieves the command key object using its ID.
   *
   * @param id
   *            The command ID.
   * @return Returns the command key object.
   */
  public static Command get(int id)
  {
    for (int i = 0; i < vecCommand.size(); i++) {
      Command command = vecCommand.get(i);
      if (command != null)
        if (command.id == id)
          return command;
    }
    return null;
  }

  /**
   * Retrieves the command key name using its ID.
   *
   * @param id
   *            The command ID.
   * @return Returns the name string or an empty string if the ID is invalid.
   */
  public static String getName(int id)
  {
    for (int i = 0; i < vecCommand.size(); i++) {
      Command command = vecCommand.get(i);
      if (command != null)
        if (command.id == id)
          return command.name;
    }
    return "";
  }

  /**
   * Creates a command menu item with an icon to a given menu.
   *
   * @param menu
   *            The menu object.
   * @param cmdId
   *            The command key ID.
   * @param iconResId
   *            The icon resource ID.
   * @return Returns a menu item object.
   */
  public static MenuItem addMenuItem(Menu menu, int cmdId, int iconResId)
  {
    MenuItem item = menu.add(0, cmdId, Menu.NONE, Command.getName(cmdId));
    if (item != null) {
      item.setIcon(iconResId);
    }
    return item;
  }

  /**
   * Creates a command sub-menu with an icon to a given menu.
   *
   * @param menu
   *            The menu object.
   * @param cmdId
   *            The command key ID.
   * @param iconResId
   *            The icon resource ID.
   * @return Returns a sub-menu item object.
   */
  public static SubMenu addSubMenuItem(Menu menu, int cmdId, int iconResId)
  {
    SubMenu item = menu.addSubMenu(0, cmdId, Menu.NONE, Command.getName(cmdId));
    if (item != null) {
      item.setIcon(iconResId);
    }
    return item;
  }

  /**
   * Creates a command menu item to a given menu.
   *
   * @param menu
   *            The menu object.
   * @param cmdId
   *            The command key ID.
   * @return Returns a menu item object.
   */
  public static MenuItem addMenuItem(Menu menu, int cmdId)
  {
    return menu.add(0, cmdId, Menu.NONE, Command.getName(cmdId));
  }
}
